package com.foreveross.mofang.serviceconsole.web.controller.core;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.mofang.serviceconsole.application.core.GroupApplication;
import com.foreveross.mofang.serviceconsole.application.dto.GroupDTO;
import com.foreveross.mofang.serviceconsole.core.ChannelGroup;
import com.foreveross.mofang.serviceconsole.web.controller.common.BaseController;

/**
 * 任务及渠道组管理controller
 * 
 * @author fb
 */
@Controller
@RequestMapping("/core/Group")
public class GroupController extends BaseController{
		
	@Inject
	private GroupApplication groupApplication;
	
	@ResponseBody
	@RequestMapping("/addChannel")
	public Map<String, Object> addChannel(@Valid GroupDTO groupDTO) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		groupApplication.saveGroup(groupDTO, getLogionUser(), ChannelGroup.class);
		result.put("result", "渠道组数据创建成功!");
		return result;
	}
		
	@ResponseBody
	@RequestMapping("/update")
	public Map<String, Object> update(@Valid GroupDTO groupDTO) {
		Map<String, Object> result = new HashMap<String, Object>();
		groupApplication.updateGroup(groupDTO, getLogionUser());
		result.put("result", "组更新成功!");
		return result;
	}
	 
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> delete(@RequestParam("ids[]") Long ids[]) {
		Map<String, Object> result = new HashMap<String, Object>();
        groupApplication.removeGroups(ids, getLogionUser());
		result.put("result", "组删除成功!");
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/get/{id}")
	public Map<String, Object> get(@PathVariable Long id) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("data", groupApplication.getGroup(id));
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/getRoots")
	public Map<String, Object> channelGetRoots() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("data", groupApplication.getRoots(ChannelGroup.class));
		return result;
	}

	@ResponseBody
	@RequestMapping("/findAll")
	public Map<String, Object> findAll() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("data", groupApplication.findAllGroup());
		return result;
	}
}
